package com.yulinlin.jdbc.session;

import com.yulinlin.core.wrapper.ISqlWrapper;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class SqlNode {

    private String sql;

    private List<Object> list;


    private static final String tag_pattern = "(#\\{)(.*?)(})";

    private static final Pattern tagPattern = Pattern.compile(tag_pattern);

    public SqlNode(ISqlWrapper wrapper) {
        this.list = new ArrayList<>();
        this.init(wrapper);
    }

    private void init(ISqlWrapper wrapper){


        this.sql  = wrapper.getSql();

        Map params =  wrapper.getParams().getParams();
        Matcher matcher= tagPattern.matcher(sql);

        while (matcher.find()) {

            String fieldName = matcher.group();
            Object value =  params.get(fieldName);
            list.add(value);

            this.sql =  this.sql.replace(fieldName," ? ");

        }


        int i = 0;
    }

    public String getSql() {
        return sql;
    }

    public List<Object> getList() {
        return list;
    }
}
